const app = new getApp();
var time = require("../../../../utils/util.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    prompt: '',
    promoter_info: {},
    type: 'promoter_commission',
    page: 1,
    distribution_list: {},

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    let that = this;
    let params = {
      page: 1,
      page_size: 10,
      type: that.data.type
    };
    app.sendRequest({
      url: 'System.Distribution.commissionDistributionList',
      data: params,
      success: function(res) {
        let distribution_list = res.data.data;
        for (let index in distribution_list) {
          distribution_list[index].create_time = time.formatTime(distribution_list[index].create_time, 'Y-M-D');
        }
        that.setData({
          distribution_list: distribution_list
        })
      }
    })

    app.sendRequest({
      url: 'Nsfx.Distribution.promoterDetail',
      data: {},
      success: function (res) {
        let promoter_info = res.data;
        that.setData({
          promoter_info: promoter_info
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    let that = this;
    let page = that.data.page;
    let type = that.data.type;

    let params = {
      page: page,
      page_size: 10,
      type: type
    };
    app.sendRequest({
      url: 'System.Distribution.commissionDistributionList',
      data: params,
      success: function (res) {
        let distribution_list = res.data.data;
        for (let index in distribution_list) {
          distribution_list[index].create_time = time.formatTime(distribution_list[index].create_time, 'Y-M-D');
        }
        if (app.checkArray(distribution_list)) {
          params.page++;
        }
        that.setData({
          distribution_list: distribution_list,
          page: params.page
        })
      }
    })
  },

  /**
   * 佣金类型切换
   */
  changeType: function(e) {
    let that = this;
    let type = e.currentTarget.dataset.type;
    that.setData({
      type: type
    })
    let params = {
      page: 1,
      page_size: 10,
      type: type
    };
    app.sendRequest({
      url: 'System.Distribution.commissionDistributionList',
      data: params,
      success: function(res) {
        let distribution_list = res.data.data;
        for (let index in distribution_list) {
          distribution_list[index].create_time = time.formatTime(distribution_list[index].create_time, 'Y-M-D');
        }
        that.setData({
          page: 2
        })
        that.setData({
          distribution_list: distribution_list
        })
      }
    })

  }
})